package com.ts.proj.controller;

import com.alibaba.fastjson.JSONArray;
import com.ts.base.controller.BaseController;
import com.ts.common.constant.ConsoleConstant;
import com.ts.proj.entity.SystemUserEntity;
import com.ts.proj.service.SystemColumnService;
import com.ts.proj.service.SystemFieldService;
import com.ts.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class TaCommonController extends BaseController {

    @Autowired
    private SystemColumnService columnService;
    @Autowired
    private SystemFieldService systemFieldService;

    @RequestMapping("/business-{router}-list")
    public ModelAndView providerList(@PathVariable("router") String router){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            Object attribute = this.getRequest().getSession().getAttribute(ConsoleConstant.BTNRIGHTS);

            List<PageData> buttonList = (List<PageData>) JSONArray.parse(attribute.toString());
            modelAndView.addObject("buttons", buttonList);

            //先从Redis里面读取-----------------SystemFieldEntity--------attributes freezAttributes-------------------
            PageData pageData = FieldListUtil.fieldsList(columnService, StringsUtil.upperCase(router)+"Entity", systemFieldService);
            List<PageData> attributes = (List<PageData>) pageData.get("all");

            List<PageData> freezAttributes = (List<PageData>) pageData.get("freeze");
            if(freezAttributes.size()!=0){
                modelAndView.addObject("freeze", pageData.get("freeze"));
            }
            //应该在这里添加列头
            modelAndView.addObject("attributes", attributes);

            modelAndView.addObject("dlg", (List<PageData>) pageData.get("dlg"));


            viewName = "business/" + router + "/" + router + "-list";
            SystemUserEntity systemUserEntity = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(),SystemUserEntity.class);
            modelAndView.addObject("userId", systemUserEntity.getId());
        }

        modelAndView.setViewName(viewName);
        return modelAndView;
    }
}
